package cn.stylefeng.guns.modular.rest;


import cn.stylefeng.guns.comm.util.DateUtils;
import cn.stylefeng.guns.modular.wordstudy.entity.Plan;
import cn.stylefeng.guns.modular.wordstudy.entity.StudyHistory;
import cn.stylefeng.guns.modular.wordstudy.entity.Word;
import cn.stylefeng.guns.modular.wordstudy.model.params.PlanParam;
import cn.stylefeng.guns.modular.wordstudy.model.params.StudyHistoryParam;
import cn.stylefeng.guns.modular.wordstudy.model.result.StudyHistoryResult;
import cn.stylefeng.guns.modular.wordstudy.service.PlanService;
import cn.stylefeng.guns.modular.wordstudy.service.StudyHistoryService;
import cn.stylefeng.guns.modular.wordstudy.service.WordService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/study/history")
public class ApiNeedTokenStudyHistoryController extends BaseController {
    @Autowired
    private WordService wordService;
    @Autowired
    private StudyHistoryService studyHistoryService;


//    @RequestMapping(value = "detail")
//    public Object detail(String openId) {
//        // 剩余时间
//        int remainder = 0;
//        Date beginDate;
//        Map map = new HashMap();
//
//        Plan plan = planService.getOne(new QueryWrapper<Plan>().eq("open_id", openId));
//        if (plan != null) {
//            Date updateTime = plan.getUpdateTime();
//            if (updateTime == null) {
//                beginDate = plan.getCreateTime();
//            } else {
//                beginDate = updateTime;
//            }
//            Integer day = plan.getDay();
//            Calendar cal = Calendar.getInstance();
//            cal.setTime(beginDate);
//            cal.add(Calendar.DATE, day);
//            remainder = DateUtils.distanceDay(cal.getTime(), DateUtils.getSystemDate());
//        }
//        map.put("plan", plan);
//        map.put("remainder", remainder);
//
//        SUCCESS_TIP.setData(map);
//        return SUCCESS_TIP;
//    }


    @PostMapping("/know")
    @ResponseBody
    public ResponseData addItem(StudyHistoryParam studyHistoryParam) {
        Map map = new HashMap();

        // 是否新词判断
        StudyHistory studyHistory = studyHistoryService.getOne(new QueryWrapper<StudyHistory>().eq("open_id", studyHistoryParam.getOpenId()).eq("word_id", studyHistoryParam.getWordId()).eq("review_flag", 2));
        if (studyHistory != null) {
            // 复习
            studyHistoryParam.setReviewFlag("1");
        } else {
            // 新单词
            studyHistoryParam.setReviewFlag("2");
        }

        StudyHistory entity = new StudyHistory();
        entity.setType("1");
        ToolUtil.copyProperties(studyHistoryParam, entity);
        this.studyHistoryService.save(entity);

        // 下一个单词
        Word word = wordService.getById(entity.getWordId());
        List<Word> words = wordService.list(new QueryWrapper<Word>().eq("type", word.getType()));
        Random random = new Random();
        int n = random.nextInt(words.size());
        word = words.get(n);

        // 学习数据
        StudyHistoryParam param = new StudyHistoryParam();
        param.setOpenId(studyHistoryParam.getOpenId());
        StudyHistoryResult studyHistoryResult = studyHistoryService.studyData(param);

        map.put("word", word);
        map.put("total", studyHistoryResult.getTotal());
        map.put("newWord", studyHistoryResult.getNewWord());
        map.put("oldWord", studyHistoryResult.getOldWord());

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }
}


